<?php

namespace App\Controller\LazyCommon;

use App\Base\BaseController4Admin;
use App\Entity\User;
use App\Model\LazyCommon\AdminLogModel;
use App\Model\LazyCommon\UserModel;
//use Model\UserBundle\Repository\UserRepository;
use App\Model\LazyCommon\UserGroupsModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;
use App\Base\BaseController;
use App\Entity\User  as Entity;
use App\Setting\LazyCommon\UserSetting;

/**
 * @route("/admin/")
 */
class UserAdminController extends BaseController4Admin
{

    /**
     * @route("User/add",name="UserPreAdd")
     */
    public function add()
    {
//        die("NOT ALLOWED !");
        //通用代码
        $this->setSetting(new UserSetting($this->getDoctrine()));
        return $this->comAdd();
    }

    /**
     * @route("User/add/do",name="UserDoAdd")
     *
     * @Method({"GET", "POST"})
     */
    public function doAdd(Request $request)
    {
        if(!$form = $this->getParamRequired("form")){return $this->json_out(0,"参数缺失");}
        $account = $form["account"];


        //检查用户名是否占用
        $UserModel = new UserModel($this->getDoctrine());
        $rst = $UserModel->getRep()->findOneBy(array("account"=>$account));

        if ($rst){
            return $this->json_out(0,"用户名已占用","");
        }

//        die("NOT ALLOWED !");
        //私有代码
        $user = New User();
        $user->setAddTime(time());
        $user->setAlipay(time())
            ->setBan(0)
            ->setPasswordPay(time())
        ;
        //通用代码
        $this->setSetting(new UserSetting($this->getDoctrine()));
        $return =  $this->comDoAdd($request,$user);
        $user->setPassword(md5($user->getPassword()));
        $user->setPasswordPay(md5($user->getPasswordPay()));
        $this->myEm()->persist($user);
        $this->myEm()->flush();
        return $return;
    }

    /**
     * @route("User/edit/{id}",name="UserPreEdit")
     */
    public function edit(Entity $entity,Request $request)
    {
        $this->setSetting(new UserSetting($this->getDoctrine()));
        return $this->comEdit($entity);
    }

    /**
     * @route("User/edit/{id}/do",name="UserDoEdit")
     *
     * @Method({"GET", "POST"})
     */
    public function doEdit(Request $request,Entity $entity)
    {
        if(!$form = $this->getParamRequired("form")){return $this->json_out(0,"参数缺失");}
        $newPsw = $form["password"];
//        var_dump($newPsw);
//        var_dump($entity);
        $uid = $entity->getId();
        $UserModel = new UserModel($this->getDoctrine());
        $orgData = $UserModel->getUserById($uid);
        $oldPsw = $orgData->getPassword();
//        var_dump($orgData->getPassword());
        $fromGroup = $orgData->getGroups();
//        if ($orgData->getPassword() != $newPsw) {
//            $entity->setPassword(md5($newPsw));
//        }

        $this->setSetting(new UserSetting($this->getDoctrine()));
        $return = $this->comDoEdit($request,$entity);
        $toGroup = $entity->getGroups();

        if ($oldPsw != $newPsw) {
            $entity->setPassword(md5($newPsw))->setPasswordPay(md5($newPsw));
            $this->myEm()->persist($entity);
            $this->myEm()->flush();
        }

        $AdminLogModel = new AdminLogModel($this->getDoctrine());
        $AdminLogModel->addLog($this->needToBeAdmin(),"修改 $uid 用户信息group:$fromGroup => $toGroup ;",$request);

        return $return;
    }

    /**
     * @route("User",name="UserLists")
     */
    public function lists()
    {
        //私有部分
        $SAMPLECategoryModel = new UserGroupsModel($this->getDoctrine());
        $classes = $SAMPLECategoryModel->getAllUserGroupsTree();
//        $classes = array();
        $tools = array("groups"    => $classes,);

        //通用部分
        $this->setSetting(new UserSetting($this->getDoctrine()->getManager()));
        return $this->comLists($tools);
    }


    /**
     * @route("User/tools",name="UserTools")
     */
    public function tools(Request $request)
    {
        $this->setSetting(new UserSetting($this->getDoctrine()));
        return $this->comTools($request);
    }

}
